<?php

namespace console\core\manager;

class InstanceManager
{

    //定义Object对象容器
    private static $_instance = [];

    //对象String容器
    private static $_container =[];

    //特殊处理不使用容器
    private static $_special   =[
        'PhpConfig','IniConfig','YamlConfig',
        'JsonConfig'
    ];

    //请求一个不可见对象调用
    static function __callStatic($name, $arguments)
    {
        //是否存在于容器或特殊处理
        if(!isset(self::$_instance[$name]) || in_array($name,self::$_special))
        {
            //得到该对象处理类
            $namespace = self::container($name);
            //实例化并存入容器
            self::$_instance[$name] = !empty($arguments) ? new $namespace($arguments[0]): new $namespace();
        }

        //返回对象
        return self::$_instance[$name];
    }

    //获得对象处理类
    private static function container($name)
    {
        //对象处理类是否存在
        if(!self::$_container)
            //获取对象处理类
            self::$_container = (new \Phalcon\Config\Adapter\Php(CONSOLE_PATH.'config/Instance.php'))->toArray();

        //所需类已存在容器直接返回
        if(isset(self::$_container[$name]))
            return self::$_container[$name];

        //否则抛出错误
        throw new \Exception("Class $name is not instance config set.");
    }
}